package com.ruoyi.table.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.table.config.TokenCile;
import com.ruoyi.table.domain.Family;
import com.ruoyi.table.domain.Middle;
import com.ruoyi.table.domain.ResponseEntity;
import com.ruoyi.table.mapper.MiddleMapper;
import com.ruoyi.table.service.FamilyService;
import com.ruoyi.table.mapper.FamilyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 */
@Service
public class FamilyServiceImpl extends ServiceImpl<FamilyMapper, Family>
    implements FamilyService{

    @Resource
    private FamilyMapper familyMapper;


    @Resource
    private MiddleMapper middleMapper;

    //审核通过  1:待审核  2:审核通过  3:审核驳回
    @Override
    public ResponseEntity pass(Integer id) {
        Family byId = this.getById(id);
        if (byId.getState() == 1){
            byId.setState(2);
        }
        familyMapper.updateById(byId);

        return new ResponseEntity("审核通过");
    }

    //审核驳回 1:待审核  2:审核通过  3:审核驳回
    @Override
    public ResponseEntity reject(Integer id) {
        Family byId = this.getById(id);
        if (byId.getState() == 1){
            byId.setState(3);
        }

        familyMapper.updateById(byId);

        return new ResponseEntity("审核驳回");
    }

    @Resource
    private AuthenticationManager authenticationManager;

    @Resource
    private TokenCile tokenCile;

    @Resource
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    //商/专家  登录
    @Override
    public ResponseEntity login(Family family) {

        //验证
        UsernamePasswordAuthenticationToken authenticationToken=
                new UsernamePasswordAuthenticationToken(family.getFamilyName(),family.getFamilyPassword());


        Authentication authenticate = authenticationManager.authenticate(authenticationToken);

        if (Objects.isNull(authenticate)){
            return new ResponseEntity("认证失败");
        }

        QueryWrapper<Family> queryWrapper=new QueryWrapper<>();
        if (StringUtils.isNotEmpty(family.getFamilyName())){
            queryWrapper.eq("family_name",family.getFamilyName());
        }
        //根据账号查询数据
        Family one = this.getOne(queryWrapper);


        if (one == null){
            return new ResponseEntity(null,"账号不存在",5000,false);
        }
//        if (!family.getFamilyBankNumber().equals(one.getFamilyBankNumber())){
//            return new ResponseEntity(null,"账号密码错误",5000,false);
//        }


        if (!bCryptPasswordEncoder.matches(family.getFamilyPassword(),one.getFamilyPassword())) {
            return new ResponseEntity(null,"账号或密码错误",5000,false);
        }
        if (one.getState() == 1){
            return new ResponseEntity(null,"待申请无法登录",5000,false);
        }
        if (one.getState() == 3){
            return new ResponseEntity(null,"被驳回无法登录",5000,false);
        }

        Map<String, Object> token = tokenCile.createToken(one);
        Map map=new HashMap();
        map.put("one",one);
        map.put("token",token);


        return new ResponseEntity(map);
    }

    @Override
    public ResponseEntity MiddleList() {

        List<Middle> list=middleMapper.MiddleList();

        return new ResponseEntity(list);
    }


}




